import http from '@/utils/http.js';

import {
	appLogin
} from './api.js';
/** 图片域名判断拼接 */
export const imgCont = (e) => {
	if (typeof e === 'string') {
		const url = e.indexOf('http') === -1 ? http.apioss + e : e;
		return url
	}
}
/** 时间暂停术 */
export const awaits = (e = 500) => {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			resolve()
		}, e)
	})
}
/** 是否登录 */
export const isLogin = ()=>{
	if(!uni.getStorageSync('token')){
		uni.navigateTo({
			url:'/pages/login/login'
		})
	}
	return  
}

/** 检查图片上传状态，并格式化处理 */
export const contImage = (e) => {
	if (!e) {
		return;
	}
	if (typeof e === 'string') {
		return e
	}
	if (Array.isArray(e) && typeof e[0] === 'string') {
		return e
	} else {
		return e.map(i => {
			if (i.path) {
				return i.path
			} else {
				uni.hideLoading()
				uni.showToast({
					title: '请删除上传失败图片'
				})
				throw new Error('存在上传失败图片')
			}
		})
	}
}
//登录
export const mLogin = async () => {
	function getcodes() {
		return new Promise((resolve, reject) => {
			uni.login({
				success: (e) => {
					resolve(e.code)
				},
				fail: (e) => {
					reject(e)
				}
			})
		})
	}
	if (uni.getStorageSync('token')) {
		return;
	}
	const wxcode = await getcodes();
	// #ifdef MP-WEIXIN
	const type = 0
	// #endif
	// #ifdef MP-ALIPAY
	const type = 1
	// #endif
	const datas = {
		code: wxcode,
		type
	}
	await awaits(100);
	const {
		data
	} = await appLogin(datas);
	if (data.token) {
		uni.setStorageSync('token', data.token)
	}
}
// 回显数据字典
export function selectDictLabel(datas, value) {
	if (value === undefined) {
		return "";
	}
	var actions = [];
	Object.keys(datas).some((key) => {
		if (datas[key].value == ('' + value)) {
			actions.push(datas[key].label);
			return true;
		}
	})
	if (actions.length === 0) {
		actions.push(value);
	}
	return actions.join('');
}
export const nav =(url)=>{
	uni.navigateTo({
		url
	})
}

export function parseQueryString(url) {
    let queryString = url.substring(url.indexOf('?') + 1); // 获取查询字符串部分
    let params = {};
    queryString.split('&').forEach(param => {
        let [key, value] = param.split('='); // 分割参数名和值
        params[decodeURIComponent(key)] = decodeURIComponent(value); // 解码并存储到对象中
    });
    return params;
}

export function maskPhoneNumber(inputString) {
    if (typeof inputString !== 'string') {
         return '';
     }
 
     // 如果字符串长度小于4，则无需隐藏
     if (inputString.length <= 4) {
         return inputString;
     }
 
     // 将最后四位替换为星号
     return inputString.slice(0, -4) + '****';
}
